package com.xjj.stack;

import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

public class M0150EvaluateReversePolishNotation {

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        Set<String> set = new HashSet<>();
        set.add("+");
        set.add("-");
        set.add("*");
        set.add("/");
        for (String str : tokens) {
            if (!set.contains(str)) {
                stack.push(Integer.valueOf(str));
            } else {
                stack.push(calculate(stack.pop(), stack.pop(), str));
            }
        }
        return stack.pop();
    }

    private int calculate(int follow, int previous, String ops) {
        switch (ops) {
            case "+":
                return previous + follow;
            case "-":
                return previous - follow;
            case "*":
                return previous * follow;
            case "/":
                return previous / follow;
            default:
                return 0;
        }
    }

}
